/*
* ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
* Without permission, no one has the right to obtain, disclose or use this software in any way.
*/

package ecarx.xsf.notificationcenter.utils;


import java.util.ArrayList;
import java.util.List;

import ecarx.eas.notification.ITyrantAction;
import ecarx.eas.notification.StatusBarTyrantNotification;
import ecarx.xsf.notificationcenter.scenecard.IPendingIntentAction;
import ecarx.xsf.notificationcenter.scenecard.ISceneCardInfoV2;
import ecarx.xsf.notificationcenter.sdk.IStatusBarAction;
import ecarx.xsf.notificationcenter.sdk.IStatusBarNotification;


public class DataParcelUtil {


    public static StatusBarTyrantNotification transform(IStatusBarNotification iStatusBarNotification) {
        return new StatusBarTyrantNotification(iStatusBarNotification.getId(),
                iStatusBarNotification.getPkgName(),
                iStatusBarNotification.getTitle(),
                iStatusBarNotification.getContent(),
                iStatusBarNotification.getContentIntent(),
                iStatusBarNotification.getDeleteIntent(),
                iStatusBarNotification.getWhen(),
                iStatusBarNotification.isTyrant(),
                getActions(iStatusBarNotification.getActions())
                , isEmpty(iStatusBarNotification.getActions()) ? "" : iStatusBarNotification.getActions().get(0).getClassName());

    }

    public static ISceneCardInfoV2 transformToCard(IStatusBarNotification notification) {

        return new ISceneCardInfoV2(notification.getId(),
                notification.getPkgName(),
                notification.isBeep(),
                notification.isTtsOn(),
                notification.isSeeSay(),
                notification.getTitle(),
                notification.getContent(),
                "",
                "",
                notification.getTag(), notification.getWhen(),
                getSceneCardActions(notification.getActions()),
                getSceneCardActions(notification.getSeeSayEventActions()),
                getSceneCardActions(notification.getSmartCardActions()));

    }

    private static List<IPendingIntentAction> getSceneCardActions(List<IStatusBarAction> actions) {
        List<IPendingIntentAction> actionList = new ArrayList<>();
        for (IStatusBarAction action : actions) {
            IPendingIntentAction pendingIntentAction = new IPendingIntentAction(action.getId(),
                    action.getText(),
                    action.getBgUrl(),
                    action.getFunctionId(),
                    action.getPendingIntent());
            actionList.add(pendingIntentAction);
        }
        return actionList;
    }


    private static List<ITyrantAction> getActions(List<IStatusBarAction> actions) {
        List<ITyrantAction> tyrantActions = new ArrayList<>();

        if (actions != null) {
            for (IStatusBarAction action : actions) {
                ITyrantAction iTyrantAction = new ITyrantAction(action.getId(), action.getText(), action.getPendingIntent(), "");
                tyrantActions.add(iTyrantAction);
            }
        }

        return tyrantActions;
    }

    private static boolean isEmpty(Object obj) {
        if (obj instanceof List) {
            return obj != null && ((List) obj).size() > 0;
        }
        return true;
    }

}
